<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />

<meta name="viewport" content="width=device-width, initial-scale=1">

<meta name="author" content="Baptiste Auguié" />

<meta name="date" content="2019-07-13" />

<title>Overview of the egg package</title>



<style type="text/css">code{white-space: pre;}</style>
<style type="text/css" data-origin="pandoc">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; position: absolute; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; }
pre.numberSource a.sourceLine:empty
  { position: absolute; }
pre.numberSource a.sourceLine::before
  { content: attr(data-line-number);
    position: absolute; left: -5em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */

</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
  var sheets = document.styleSheets;
  for (var i = 0; i < sheets.length; i++) {
    if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
    try { var rules = sheets[i].cssRules; } catch (e) { continue; }
    for (var j = 0; j < rules.length; j++) {
      var rule = rules[j];
      // check if there is a div.sourceCode rule
      if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue;
      var style = rule.style.cssText;
      // check if color or background-color is set
      if (rule.style.color === '' || rule.style.backgroundColor === '') continue;
      // replace div.sourceCode by a pre.sourceCode rule
      sheets[i].deleteRule(j);
      sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
    }
  }
})();
</script>



<style type="text/css">body {
background-color: #fff;
margin: 1em auto;
max-width: 700px;
overflow: visible;
padding-left: 2em;
padding-right: 2em;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.35;
}
#header {
text-align: center;
}
#TOC {
clear: both;
margin: 0 0 10px 10px;
padding: 4px;
width: 400px;
border: 1px solid #CCCCCC;
border-radius: 5px;
background-color: #f6f6f6;
font-size: 13px;
line-height: 1.3;
}
#TOC .toctitle {
font-weight: bold;
font-size: 15px;
margin-left: 5px;
}
#TOC ul {
padding-left: 40px;
margin-left: -1.5em;
margin-top: 5px;
margin-bottom: 5px;
}
#TOC ul ul {
margin-left: -2em;
}
#TOC li {
line-height: 16px;
}
table {
margin: 1em auto;
border-width: 1px;
border-color: #DDDDDD;
border-style: outset;
border-collapse: collapse;
}
table th {
border-width: 2px;
padding: 5px;
border-style: inset;
}
table td {
border-width: 1px;
border-style: inset;
line-height: 18px;
padding: 5px 5px;
}
table, table th, table td {
border-left-style: none;
border-right-style: none;
}
table thead, table tr.even {
background-color: #f7f7f7;
}
p {
margin: 0.5em 0;
}
blockquote {
background-color: #f6f6f6;
padding: 0.25em 0.75em;
}
hr {
border-style: solid;
border: none;
border-top: 1px solid #777;
margin: 28px 0;
}
dl {
margin-left: 0;
}
dl dd {
margin-bottom: 13px;
margin-left: 13px;
}
dl dt {
font-weight: bold;
}
ul {
margin-top: 0;
}
ul li {
list-style: circle outside;
}
ul ul {
margin-bottom: 0;
}
pre, code {
background-color: #f7f7f7;
border-radius: 3px;
color: #333;
white-space: pre-wrap; 
}
pre {
border-radius: 3px;
margin: 5px 0px 10px 0px;
padding: 10px;
}
pre:not([class]) {
background-color: #f7f7f7;
}
code {
font-family: Consolas, Monaco, 'Courier New', monospace;
font-size: 85%;
}
p > code, li > code {
padding: 2px 0px;
}
div.figure {
text-align: center;
}
img {
background-color: #FFFFFF;
padding: 2px;
border: 1px solid #DDDDDD;
border-radius: 3px;
border: 1px solid #CCCCCC;
margin: 0 5px;
}
h1 {
margin-top: 0;
font-size: 35px;
line-height: 40px;
}
h2 {
border-bottom: 4px solid #f7f7f7;
padding-top: 10px;
padding-bottom: 2px;
font-size: 145%;
}
h3 {
border-bottom: 2px solid #f7f7f7;
padding-top: 10px;
font-size: 120%;
}
h4 {
border-bottom: 1px solid #f7f7f7;
margin-left: 8px;
font-size: 105%;
}
h5, h6 {
border-bottom: 1px solid #ccc;
font-size: 105%;
}
a {
color: #0033dd;
text-decoration: none;
}
a:hover {
color: #6666ff; }
a:visited {
color: #800080; }
a:visited:hover {
color: #BB00BB; }
a[href^="http:"] {
text-decoration: underline; }
a[href^="https:"] {
text-decoration: underline; }

code > span.kw { color: #555; font-weight: bold; } 
code > span.dt { color: #902000; } 
code > span.dv { color: #40a070; } 
code > span.bn { color: #d14; } 
code > span.fl { color: #d14; } 
code > span.ch { color: #d14; } 
code > span.st { color: #d14; } 
code > span.co { color: #888888; font-style: italic; } 
code > span.ot { color: #007020; } 
code > span.al { color: #ff0000; font-weight: bold; } 
code > span.fu { color: #900; font-weight: bold; }  code > span.er { color: #a61717; background-color: #e3d2d2; } 
</style>




</head>

<body>




<h1 class="title toc-ignore">Overview of the egg package</h1>
<h4 class="author">Baptiste Auguié</h4>
<h4 class="date">2019-07-13</h4>



<p>This document provides a brief overview of the main utiilty functions included in the <code>egg</code> package.</p>
<div id="exposing-ggplot2-layouts" class="section level2">
<h2>Exposing ggplot2 layouts</h2>
<p>The function <code>expose_layout</code> can be useful to illustrate the structure of ggplot2 plots, e.g. when ying to customise and/or post-process the gtable layout.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" data-line-number="1">p1 &lt;-<span class="st"> </span><span class="kw">qplot</span>(mpg, wt, <span class="dt">data =</span> mtcars, <span class="dt">colour =</span> cyl)</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">p2 &lt;-<span class="st"> </span><span class="kw">qplot</span>(mpg, <span class="dt">data =</span> mtcars) <span class="op">+</span><span class="st"> </span><span class="kw">ggtitle</span>(<span class="st">&quot;title&quot;</span>)</a>
<a class="sourceLine" id="cb1-3" data-line-number="3">p3 &lt;-<span class="st"> </span><span class="kw">qplot</span>(mpg, <span class="dt">data =</span> mtcars, <span class="dt">geom =</span> <span class="st">&quot;dotplot&quot;</span>)</a>
<a class="sourceLine" id="cb1-4" data-line-number="4">p4 &lt;-</a>
<a class="sourceLine" id="cb1-5" data-line-number="5">p1 <span class="op">+</span><span class="st"> </span><span class="kw">facet_wrap</span>( <span class="op">~</span><span class="st"> </span>carb, <span class="dt">nrow =</span> <span class="dv">1</span>) <span class="op">+</span><span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="st">&quot;none&quot;</span>) <span class="op">+</span></a>
<a class="sourceLine" id="cb1-6" data-line-number="6"><span class="kw">ggtitle</span>(<span class="st">&quot;facetted plot&quot;</span>)</a>
<a class="sourceLine" id="cb1-7" data-line-number="7">pl &lt;-<span class="st"> </span><span class="kw">lapply</span>(<span class="kw">list</span>(p1, p2, p3, p4), expose_layout, <span class="ot">FALSE</span>, <span class="ot">FALSE</span>)</a>
<a class="sourceLine" id="cb1-8" data-line-number="8"><span class="kw">grid.arrange</span>(</a>
<a class="sourceLine" id="cb1-9" data-line-number="9"><span class="dt">grobs =</span> pl,</a>
<a class="sourceLine" id="cb1-10" data-line-number="10"><span class="dt">widths =</span> <span class="kw">c</span>(<span class="fl">1.2</span>, <span class="dv">1</span>, <span class="dv">1</span>),</a>
<a class="sourceLine" id="cb1-11" data-line-number="11"><span class="dt">layout_matrix =</span> <span class="kw">rbind</span>(<span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>),</a>
<a class="sourceLine" id="cb1-12" data-line-number="12"><span class="kw">c</span>(<span class="dv">4</span>, <span class="dv">4</span>, <span class="dv">4</span>))</a>
<a class="sourceLine" id="cb1-13" data-line-number="13">)</a></code></pre></div>
<p><img src="" /><!-- --></p>
</div>
<div id="setting-panel-size" class="section level2">
<h2>Setting panel size</h2>
<p>In some cases, having ggplot2 expand the plot panel to best fit the available space isn’t ideal: for instance, we may want to produce multiple plots to appear on different slides of a presentation, and the successive pages should have the exact same layout for smooth visual transition. Another use-case is to embed multiple separate graphics in a drawing/page layout software. In this situation the plot alignement will be made manually, but the plots should not be rescaled (otherwise the fonts would be distorted). For such situations, the easiest solution is to set fixed dimensions to the gtable produced by ggplot2.</p>
<p>The function <code>set_panel_size</code> helps set the panel size (width, height) to absolute measurements in the form of grid units. In the case of a facetted plot, all panels are set to the same value.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" data-line-number="1">  p1 &lt;-<span class="st"> </span><span class="kw">qplot</span>(mpg, wt, <span class="dt">data =</span> mtcars, <span class="dt">colour =</span> cyl)</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">p2 &lt;-<span class="st"> </span>p1 <span class="op">+</span><span class="st"> </span><span class="kw">facet_wrap</span>( <span class="op">~</span><span class="st"> </span>carb, <span class="dt">nrow =</span> <span class="dv">1</span>)</a>
<a class="sourceLine" id="cb2-3" data-line-number="3"><span class="kw">grid.arrange</span>(<span class="dt">grobs =</span> <span class="kw">lapply</span>(</a>
<a class="sourceLine" id="cb2-4" data-line-number="4"><span class="kw">list</span>(p1, p2),</a>
<a class="sourceLine" id="cb2-5" data-line-number="5">set_panel_size,</a>
<a class="sourceLine" id="cb2-6" data-line-number="6"><span class="dt">width =</span> <span class="kw">unit</span>(<span class="dv">2</span>, <span class="st">&quot;cm&quot;</span>),</a>
<a class="sourceLine" id="cb2-7" data-line-number="7"><span class="dt">height =</span> <span class="kw">unit</span>(<span class="dv">1</span>, <span class="st">&quot;in&quot;</span>)</a>
<a class="sourceLine" id="cb2-8" data-line-number="8">))</a></code></pre></div>
<p><img src="" /><!-- --></p>
<p>Note that the total size is now fixed, therefore when exporting the plot on a device it can be useful to query the size and set the width and height accordingly, to avoid clipping or white margins. This extra step is enabled by default when saving the results to a file.</p>
</div>
<div id="aligning-complex-ggplots" class="section level2">
<h2>Aligning complex ggplots</h2>
<p><code>gridExtra::grid.arrange</code> provides no way to align the panels of individual plots. While this is achievable with low-level <code>gtable</code> functions, it often requires substantial effort on a case-by-case basis. The <code>egg</code> package introduces a general strategy for such layout manipulations, with the following steps:</p>
<ul>
<li>decompose each plot into a 3x3 layout, where the central cell corresponds to the core panels, surrounded by axes, legends, etc.</li>
<li>set the core width and height to a fixed dimension</li>
<li>align the individual 3x3 gtables using <code>rbind</code>/<code>cbind</code></li>
</ul>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb3-1" data-line-number="1">p1 &lt;-<span class="st"> </span><span class="kw">ggplot</span>(mtcars, <span class="kw">aes</span>(mpg, wt, <span class="dt">colour =</span> <span class="kw">factor</span>(cyl))) <span class="op">+</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2"><span class="st">  </span><span class="kw">geom_point</span>()</a>
<a class="sourceLine" id="cb3-3" data-line-number="3"></a>
<a class="sourceLine" id="cb3-4" data-line-number="4">p2 &lt;-<span class="st"> </span><span class="kw">ggplot</span>(mtcars, <span class="kw">aes</span>(mpg, wt, <span class="dt">colour =</span> <span class="kw">factor</span>(cyl))) <span class="op">+</span></a>
<a class="sourceLine" id="cb3-5" data-line-number="5"><span class="st">  </span><span class="kw">geom_point</span>() <span class="op">+</span><span class="st"> </span><span class="kw">facet_wrap</span>(<span class="op">~</span><span class="st"> </span>cyl, <span class="dt">ncol =</span> <span class="dv">2</span>, <span class="dt">scales =</span> <span class="st">&quot;free&quot;</span>) <span class="op">+</span></a>
<a class="sourceLine" id="cb3-6" data-line-number="6"><span class="st">  </span><span class="kw">guides</span>(<span class="dt">colour =</span> <span class="st">&quot;none&quot;</span>) <span class="op">+</span></a>
<a class="sourceLine" id="cb3-7" data-line-number="7"><span class="st">  </span><span class="kw">theme</span>()</a>
<a class="sourceLine" id="cb3-8" data-line-number="8"></a>
<a class="sourceLine" id="cb3-9" data-line-number="9">p3 &lt;-<span class="st"> </span><span class="kw">ggplot</span>(mtcars, <span class="kw">aes</span>(mpg, wt, <span class="dt">colour =</span> <span class="kw">factor</span>(cyl))) <span class="op">+</span></a>
<a class="sourceLine" id="cb3-10" data-line-number="10"><span class="st">  </span><span class="kw">geom_point</span>() <span class="op">+</span><span class="st"> </span><span class="kw">facet_grid</span>(. <span class="op">~</span><span class="st"> </span>cyl, <span class="dt">scales =</span> <span class="st">&quot;free&quot;</span>)</a>
<a class="sourceLine" id="cb3-11" data-line-number="11"></a>
<a class="sourceLine" id="cb3-12" data-line-number="12">g1 &lt;-<span class="st"> </span><span class="kw">ggplotGrob</span>(p1)</a>
<a class="sourceLine" id="cb3-13" data-line-number="13">g2 &lt;-<span class="st"> </span><span class="kw">ggplotGrob</span>(p2)</a>
<a class="sourceLine" id="cb3-14" data-line-number="14">g3 &lt;-<span class="st"> </span><span class="kw">ggplotGrob</span>(p3)</a>
<a class="sourceLine" id="cb3-15" data-line-number="15"></a>
<a class="sourceLine" id="cb3-16" data-line-number="16">fg1 &lt;-<span class="st"> </span><span class="kw">gtable_frame</span>(g1, <span class="dt">debug =</span> <span class="ot">TRUE</span>)</a>
<a class="sourceLine" id="cb3-17" data-line-number="17">fg2 &lt;-<span class="st"> </span><span class="kw">gtable_frame</span>(g2, <span class="dt">debug =</span> <span class="ot">TRUE</span>)</a>
<a class="sourceLine" id="cb3-18" data-line-number="18">fg12 &lt;-</a>
<a class="sourceLine" id="cb3-19" data-line-number="19"><span class="st">  </span><span class="kw">gtable_frame</span>(<span class="kw">gtable_rbind</span>(fg1, fg2),</a>
<a class="sourceLine" id="cb3-20" data-line-number="20">               <span class="dt">width =</span> <span class="kw">unit</span>(<span class="dv">2</span>, <span class="st">&quot;null&quot;</span>),</a>
<a class="sourceLine" id="cb3-21" data-line-number="21">               <span class="dt">height =</span> <span class="kw">unit</span>(<span class="dv">1</span>, <span class="st">&quot;null&quot;</span>))</a>
<a class="sourceLine" id="cb3-22" data-line-number="22">fg3 &lt;-</a>
<a class="sourceLine" id="cb3-23" data-line-number="23"><span class="st">  </span><span class="kw">gtable_frame</span>(</a>
<a class="sourceLine" id="cb3-24" data-line-number="24">    g3,</a>
<a class="sourceLine" id="cb3-25" data-line-number="25">    <span class="dt">width =</span> <span class="kw">unit</span>(<span class="dv">1</span>, <span class="st">&quot;null&quot;</span>),</a>
<a class="sourceLine" id="cb3-26" data-line-number="26">    <span class="dt">height =</span> <span class="kw">unit</span>(<span class="dv">1</span>, <span class="st">&quot;null&quot;</span>),</a>
<a class="sourceLine" id="cb3-27" data-line-number="27">    <span class="dt">debug =</span> <span class="ot">TRUE</span></a>
<a class="sourceLine" id="cb3-28" data-line-number="28">  )</a>
<a class="sourceLine" id="cb3-29" data-line-number="29"><span class="kw">grid.newpage</span>()</a>
<a class="sourceLine" id="cb3-30" data-line-number="30">combined &lt;-<span class="st"> </span><span class="kw">gtable_cbind</span>(fg12, fg3)</a>
<a class="sourceLine" id="cb3-31" data-line-number="31"><span class="kw">grid.draw</span>(combined)</a></code></pre></div>
<p><img src="" /><!-- --></p>
</div>
<div id="arranging-and-aligning-multiple-plots" class="section level2">
<h2>Arranging and aligning multiple plots</h2>
<p>Using this generic strategy, we can easily align arbitrary plots (facetted or single-panel), with the convenience function <code>ggarrange</code>,</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb4-1" data-line-number="1">p1 &lt;-<span class="st"> </span><span class="kw">ggplot</span>(mtcars, <span class="kw">aes</span>(mpg, wt, <span class="dt">colour =</span> <span class="kw">factor</span>(cyl))) <span class="op">+</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2"><span class="st">  </span><span class="kw">geom_point</span>()<span class="op">+</span><span class="st"> </span><span class="kw">theme_article</span>() <span class="op">+</span><span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="st">'top'</span>) </a>
<a class="sourceLine" id="cb4-3" data-line-number="3">p2 &lt;-<span class="st"> </span><span class="kw">ggplot</span>(mtcars, <span class="kw">aes</span>(mpg, wt, <span class="dt">colour =</span> <span class="kw">factor</span>(cyl))) <span class="op">+</span></a>
<a class="sourceLine" id="cb4-4" data-line-number="4"><span class="st">  </span><span class="kw">geom_point</span>() <span class="op">+</span><span class="st"> </span><span class="kw">facet_wrap</span>(<span class="op">~</span><span class="st"> </span>cyl, <span class="dt">ncol =</span> <span class="dv">2</span>, <span class="dt">scales =</span> <span class="st">&quot;free&quot;</span>) <span class="op">+</span></a>
<a class="sourceLine" id="cb4-5" data-line-number="5"><span class="st">  </span><span class="kw">guides</span>(<span class="dt">colour =</span> <span class="st">&quot;none&quot;</span>) <span class="op">+</span></a>
<a class="sourceLine" id="cb4-6" data-line-number="6"><span class="st">  </span><span class="kw">theme_article</span>()</a>
<a class="sourceLine" id="cb4-7" data-line-number="7">  </a>
<a class="sourceLine" id="cb4-8" data-line-number="8"><span class="kw">ggarrange</span>(p1, p2, <span class="dt">widths =</span> <span class="kw">c</span>(<span class="fl">1.5</span>,<span class="dv">2</span>))</a></code></pre></div>
<p><img src="" /><!-- --></p>
<p>Note that custom widths and heights may be provided for the layout.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb5-1" data-line-number="1">p &lt;-<span class="st"> </span><span class="kw">ggplot</span>()</a>
<a class="sourceLine" id="cb5-2" data-line-number="2"><span class="kw">ggarrange</span>(p, p, p, <span class="dt">widths =</span> <span class="kw">c</span>(<span class="dv">3</span>, <span class="dv">1</span>), <span class="dt">heights =</span> <span class="kw">c</span>(<span class="dv">5</span>, <span class="dv">1</span>))</a></code></pre></div>
<p><img src="" /><!-- --></p>
<p>For convenience, labels can be added to refer to the subfigures. All parameters of <code>textGrob</code> can be used for the formatting of the labels, including the positioning (<code>x</code>, <code>hjust</code>, etc.).</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="kw">ggarrange</span>(p1, p2, p3, <span class="dt">ncol=</span><span class="dv">2</span>,</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">          <span class="dt">labels =</span> <span class="kw">c</span>(<span class="st">&quot;A&quot;</span>, <span class="st">&quot;b)&quot;</span>, <span class="st">&quot;iii.&quot;</span>), </a>
<a class="sourceLine" id="cb6-3" data-line-number="3">          <span class="dt">label.args =</span> <span class="kw">list</span>(<span class="dt">gp=</span><span class="kw">gpar</span>(<span class="dt">font=</span><span class="dv">4</span>), <span class="dt">x=</span><span class="kw">unit</span>(<span class="dv">1</span>,<span class="st">&quot;line&quot;</span>), <span class="dt">hjust=</span><span class="dv">0</span>))</a></code></pre></div>
<p><img src="" /><!-- --></p>
<p>The package provides two functions for labelling facetted plots in a more compact manner, removing panel strips and using in-panel tags instead:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb7-1" data-line-number="1">d =<span class="st"> </span><span class="kw">data.frame</span>(</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">  <span class="dt">x =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">90</span>,</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">  <span class="dt">y =</span> <span class="kw">rnorm</span>(<span class="dv">90</span>),</a>
<a class="sourceLine" id="cb7-4" data-line-number="4">  <span class="dt">red =</span> <span class="kw">rep</span>(letters[<span class="dv">1</span><span class="op">:</span><span class="dv">3</span>], <span class="dv">30</span>),</a>
<a class="sourceLine" id="cb7-5" data-line-number="5">  <span class="dt">blue =</span> <span class="kw">c</span>(<span class="kw">rep</span>(<span class="dv">1</span>, <span class="dv">30</span>), <span class="kw">rep</span>(<span class="dv">2</span>, <span class="dv">30</span>), <span class="kw">rep</span>(<span class="dv">3</span>, <span class="dv">30</span>)))</a>
<a class="sourceLine" id="cb7-6" data-line-number="6"></a>
<a class="sourceLine" id="cb7-7" data-line-number="7">p &lt;-<span class="st"> </span><span class="kw">ggplot</span>(d) <span class="op">+</span></a>
<a class="sourceLine" id="cb7-8" data-line-number="8"><span class="st">  </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">x =</span> x, <span class="dt">y =</span> y)) <span class="op">+</span></a>
<a class="sourceLine" id="cb7-9" data-line-number="9"><span class="st">  </span><span class="kw">facet_grid</span>(red <span class="op">~</span><span class="st"> </span>blue)</a>
<a class="sourceLine" id="cb7-10" data-line-number="10">  </a>
<a class="sourceLine" id="cb7-11" data-line-number="11"><span class="kw">tag_facet</span>(p)</a></code></pre></div>
<p><img src="" /><!-- --></p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb8-1" data-line-number="1"><span class="kw">tag_facet_outside</span>(p)</a></code></pre></div>
<p><img src="" /><!-- --></p>
</div>
<div id="custom-themes" class="section level2">
<h2>Custom themes</h2>
<p><code>egg</code> provides <code>theme_article</code> and <code>theme_presentation</code>,</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb9-1" data-line-number="1">d =<span class="st"> </span><span class="kw">data.frame</span>(</a>
<a class="sourceLine" id="cb9-2" data-line-number="2">  <span class="dt">x =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">90</span>,</a>
<a class="sourceLine" id="cb9-3" data-line-number="3">  <span class="dt">y =</span> <span class="kw">rnorm</span>(<span class="dv">90</span>),</a>
<a class="sourceLine" id="cb9-4" data-line-number="4">  <span class="dt">red =</span> <span class="kw">rep</span>(letters[<span class="dv">1</span><span class="op">:</span><span class="dv">3</span>], <span class="dv">30</span>),</a>
<a class="sourceLine" id="cb9-5" data-line-number="5">  <span class="dt">blue =</span> <span class="kw">c</span>(<span class="kw">rep</span>(<span class="dv">1</span>, <span class="dv">30</span>), <span class="kw">rep</span>(<span class="dv">2</span>, <span class="dv">30</span>), <span class="kw">rep</span>(<span class="dv">3</span>, <span class="dv">30</span>)))</a>
<a class="sourceLine" id="cb9-6" data-line-number="6"></a>
<a class="sourceLine" id="cb9-7" data-line-number="7">p &lt;-<span class="st"> </span><span class="kw">ggplot</span>(d) <span class="op">+</span></a>
<a class="sourceLine" id="cb9-8" data-line-number="8"><span class="st">  </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(<span class="dt">x =</span> x, <span class="dt">y =</span> y)) <span class="op">+</span></a>
<a class="sourceLine" id="cb9-9" data-line-number="9"><span class="st">  </span><span class="kw">facet_grid</span>(red <span class="op">~</span><span class="st"> </span>blue)</a>
<a class="sourceLine" id="cb9-10" data-line-number="10"></a>
<a class="sourceLine" id="cb9-11" data-line-number="11">p <span class="op">+</span><span class="st"> </span><span class="kw">theme_article</span>()</a></code></pre></div>
<p><img src="" /><!-- --></p>
</div>
<div id="symmetric-axis-scale" class="section level2">
<h2>Symmetric axis scale</h2>
<p>The function <code>symmetric_range</code> helps align the 0 value of adjacent panels in facetted plots with asymmetric range of data in each group.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb10-1" data-line-number="1">df =<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">x =</span> <span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>),</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">                <span class="dt">y =</span> <span class="kw">c</span>(<span class="dv">5</span>, <span class="fl">0.2</span>),</a>
<a class="sourceLine" id="cb10-3" data-line-number="3">                <span class="dt">group =</span> <span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>))</a>
<a class="sourceLine" id="cb10-4" data-line-number="4">p &lt;-<span class="st"> </span><span class="kw">ggplot</span>(df, <span class="kw">aes</span>(<span class="dt">x =</span> x, <span class="dt">y =</span> y)) <span class="op">+</span><span class="st"> </span></a>
<a class="sourceLine" id="cb10-5" data-line-number="5"><span class="st">  </span><span class="kw">geom_point</span>() <span class="op">+</span><span class="st"> </span></a>
<a class="sourceLine" id="cb10-6" data-line-number="6"><span class="st">  </span><span class="kw">facet_wrap</span>( <span class="op">~</span><span class="st"> </span>group, <span class="dt">scale =</span></a>
<a class="sourceLine" id="cb10-7" data-line-number="7">                <span class="st">&quot;free&quot;</span>)</a>
<a class="sourceLine" id="cb10-8" data-line-number="8">p <span class="op">+</span><span class="st"> </span><span class="kw">scale_y_continuous</span>(<span class="dt">limits =</span> symmetric_range)</a></code></pre></div>
<p><img src="" /><!-- --></p>
</div>
<div id="custom-geom" class="section level2">
<h2>Custom geom</h2>
<p>The function <code>geom_custom</code> extends the ggplot2 function <code>annotation_custom</code> to cases where multiple grobs are to be placed, e.g. on different panels, or at different positions in a plot. This geom is a bit special in that it does not truly respect a <em>grammar of graphics</em> – arbitrary grobs can be plotted, with no explicit mapping to variables. Its typical use would be to place annotations (images, tables, …). The data used to create the annotation is passed as a list-column.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb11-1" data-line-number="1">codes &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">country =</span> <span class="kw">c</span>(<span class="st">&quot;nz&quot;</span>,<span class="st">&quot;ca&quot;</span>,<span class="st">&quot;ar&quot;</span>,<span class="st">&quot;fr&quot;</span>,<span class="st">&quot;gb&quot;</span>,<span class="st">&quot;es&quot;</span>))</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">codes<span class="op">$</span>y &lt;-<span class="st"> </span><span class="kw">runif</span>(<span class="kw">nrow</span>(codes))</a>
<a class="sourceLine" id="cb11-3" data-line-number="3"></a>
<a class="sourceLine" id="cb11-4" data-line-number="4">gl &lt;-<span class="st"> </span><span class="kw">lapply</span>(codes<span class="op">$</span>country, </a>
<a class="sourceLine" id="cb11-5" data-line-number="5">                    <span class="cf">function</span>(.x) png<span class="op">::</span><span class="kw">readPNG</span>(<span class="kw">system.file</span>(<span class="st">&quot;flags&quot;</span>, </a>
<a class="sourceLine" id="cb11-6" data-line-number="6">                                             <span class="kw">paste0</span>(.x,<span class="st">&quot;.png&quot;</span>),</a>
<a class="sourceLine" id="cb11-7" data-line-number="7">                                             <span class="dt">package=</span><span class="st">&quot;egg&quot;</span>)))</a>
<a class="sourceLine" id="cb11-8" data-line-number="8"></a>
<a class="sourceLine" id="cb11-9" data-line-number="9">codes<span class="op">$</span>raster &lt;-<span class="st"> </span><span class="kw">I</span>(gl)</a>
<a class="sourceLine" id="cb11-10" data-line-number="10"></a>
<a class="sourceLine" id="cb11-11" data-line-number="11"></a>
<a class="sourceLine" id="cb11-12" data-line-number="12"><span class="kw">ggplot</span>(codes, <span class="kw">aes</span>(<span class="dt">x =</span> country, <span class="dt">y =</span> y)) <span class="op">+</span><span class="st"> </span></a>
<a class="sourceLine" id="cb11-13" data-line-number="13"><span class="st">  </span><span class="kw">geom_point</span>() <span class="op">+</span><span class="st"> </span></a>
<a class="sourceLine" id="cb11-14" data-line-number="14"><span class="st">  </span><span class="kw">geom_custom</span>(<span class="dt">data =</span> codes, <span class="kw">aes</span>(<span class="dt">data=</span>raster), </a>
<a class="sourceLine" id="cb11-15" data-line-number="15">              <span class="dt">grob_fun =</span> rasterGrob, </a>
<a class="sourceLine" id="cb11-16" data-line-number="16">              <span class="dt">fun_params =</span> <span class="kw">list</span>(<span class="dt">height=</span><span class="kw">unit</span>(<span class="dv">1</span>,<span class="st">&quot;cm&quot;</span>))) <span class="op">+</span></a>
<a class="sourceLine" id="cb11-17" data-line-number="17"><span class="st">  </span><span class="kw">scale_y_continuous</span>(<span class="dt">breaks=</span><span class="ot">NULL</span>, <span class="st">&quot;&quot;</span>) <span class="op">+</span></a>
<a class="sourceLine" id="cb11-18" data-line-number="18"><span class="st">  </span><span class="kw">theme</span>(<span class="dt">panel.grid =</span> <span class="kw">element_blank</span>())</a></code></pre></div>
<p><img src="" /><!-- --></p>
<p>The list-column format allows passing grobs directly, in which case the <code>grob_fun</code> function should be identity,</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb12-1" data-line-number="1">codes<span class="op">$</span>raster &lt;-<span class="st"> </span><span class="kw">I</span>(<span class="kw">lapply</span>(codes<span class="op">$</span>raster, <span class="cf">function</span>(x) <span class="kw">rasterGrob</span>(x, <span class="dt">height=</span><span class="kw">unit</span>(<span class="dv">1</span>,<span class="st">&quot;cm&quot;</span>))))</a>
<a class="sourceLine" id="cb12-2" data-line-number="2"></a>
<a class="sourceLine" id="cb12-3" data-line-number="3"><span class="kw">ggplot</span>(codes, <span class="kw">aes</span>(<span class="dt">x =</span> country, <span class="dt">y =</span> y)) <span class="op">+</span><span class="st"> </span></a>
<a class="sourceLine" id="cb12-4" data-line-number="4"><span class="st">  </span><span class="kw">geom_point</span>() <span class="op">+</span></a>
<a class="sourceLine" id="cb12-5" data-line-number="5"><span class="st">  </span><span class="kw">geom_custom</span>(<span class="dt">data =</span> codes, <span class="kw">aes</span>(<span class="dt">data=</span>raster), </a>
<a class="sourceLine" id="cb12-6" data-line-number="6">              <span class="dt">grob_fun =</span> identity)</a></code></pre></div>
<p><img src="" /><!-- --></p>
<p>Note that such grobs need to have <code>x</code> and <code>y</code> slots, which will be mapped to the appropriate location. It is therefore often necessary to create a wrapper with such fields, as illustrated below.</p>
<p>Because the grobs are manually “mapped”, independently of the main ggplot, this geom also allows the placing of arbitrary annotations without interference from transformed coordinate systems, etc.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb13-1" data-line-number="1">custom_grob &lt;-<span class="st"> </span><span class="cf">function</span>(data, <span class="dt">x=</span><span class="fl">0.5</span>,<span class="dt">y=</span><span class="fl">0.5</span>){</a>
<a class="sourceLine" id="cb13-2" data-line-number="2">  <span class="kw">grob</span>(<span class="dt">data=</span>data,<span class="dt">x=</span>x,<span class="dt">y=</span>y, <span class="dt">cl=</span><span class="st">&quot;custom&quot;</span>)</a>
<a class="sourceLine" id="cb13-3" data-line-number="3">}</a>
<a class="sourceLine" id="cb13-4" data-line-number="4">preDrawDetails.custom &lt;-<span class="st"> </span><span class="cf">function</span>(x){</a>
<a class="sourceLine" id="cb13-5" data-line-number="5">  <span class="kw">pushViewport</span>(<span class="kw">viewport</span>(<span class="dt">x=</span>x<span class="op">$</span>x,<span class="dt">y=</span>x<span class="op">$</span>y))</a>
<a class="sourceLine" id="cb13-6" data-line-number="6">}</a>
<a class="sourceLine" id="cb13-7" data-line-number="7">postDrawDetails.custom &lt;-<span class="st"> </span><span class="cf">function</span>(x){</a>
<a class="sourceLine" id="cb13-8" data-line-number="8">  <span class="kw">upViewport</span>()</a>
<a class="sourceLine" id="cb13-9" data-line-number="9">}</a>
<a class="sourceLine" id="cb13-10" data-line-number="10">drawDetails.custom &lt;-<span class="st"> </span><span class="cf">function</span>(x, <span class="dt">recording=</span><span class="ot">FALSE</span>, ...){</a>
<a class="sourceLine" id="cb13-11" data-line-number="11">  <span class="kw">grid.rect</span>(<span class="kw">mean</span>(x<span class="op">$</span>data<span class="op">$</span>x), <span class="kw">mean</span>(x<span class="op">$</span>data<span class="op">$</span>y), </a>
<a class="sourceLine" id="cb13-12" data-line-number="12">            <span class="dt">width=</span><span class="kw">diff</span>(<span class="kw">range</span>(x<span class="op">$</span>data<span class="op">$</span>x)), </a>
<a class="sourceLine" id="cb13-13" data-line-number="13">            <span class="dt">height=</span><span class="kw">diff</span>(<span class="kw">range</span>(x<span class="op">$</span>data<span class="op">$</span>y)))</a>
<a class="sourceLine" id="cb13-14" data-line-number="14">  <span class="kw">grid.lines</span>(x<span class="op">$</span>data<span class="op">$</span>x, x<span class="op">$</span>data<span class="op">$</span>y, <span class="dt">gp=</span><span class="kw">gpar</span>(<span class="dt">col=</span>x<span class="op">$</span>data<span class="op">$</span>col,<span class="dt">lwd=</span><span class="dv">2</span>), <span class="dt">default.units =</span> <span class="st">&quot;native&quot;</span>)</a>
<a class="sourceLine" id="cb13-15" data-line-number="15">}</a>
<a class="sourceLine" id="cb13-16" data-line-number="16"></a>
<a class="sourceLine" id="cb13-17" data-line-number="17">d &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">x=</span><span class="kw">rep</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">3</span>, <span class="dv">4</span>), <span class="dt">f=</span><span class="kw">rep</span>(letters[<span class="dv">1</span><span class="op">:</span><span class="dv">4</span>], <span class="dt">each=</span><span class="dv">3</span>))</a>
<a class="sourceLine" id="cb13-18" data-line-number="18">gl &lt;-<span class="st"> </span><span class="kw">lapply</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">4</span>, <span class="cf">function</span>(ii){</a>
<a class="sourceLine" id="cb13-19" data-line-number="19">  <span class="kw">data.frame</span>(<span class="dt">x=</span><span class="kw">seq</span>(<span class="fl">0.4</span>,<span class="fl">0.6</span>,<span class="dt">length=</span><span class="dv">10</span>),</a>
<a class="sourceLine" id="cb13-20" data-line-number="20">             <span class="dt">y =</span> <span class="kw">runif</span>(<span class="dv">10</span>,<span class="fl">0.45</span>,<span class="fl">0.55</span>),</a>
<a class="sourceLine" id="cb13-21" data-line-number="21">             <span class="dt">col =</span> <span class="kw">hcl</span>(<span class="dt">h =</span> <span class="kw">seq</span>(<span class="dv">0</span>,<span class="dv">300</span>,<span class="dt">length=</span><span class="kw">nrow</span>(d)))[ii],</a>
<a class="sourceLine" id="cb13-22" data-line-number="22">             <span class="dt">stringsAsFactors =</span> <span class="ot">FALSE</span>)</a>
<a class="sourceLine" id="cb13-23" data-line-number="23">})</a>
<a class="sourceLine" id="cb13-24" data-line-number="24">subplots &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">f=</span>letters[<span class="dv">1</span><span class="op">:</span><span class="dv">4</span>], <span class="dt">data =</span> <span class="kw">I</span>(gl))</a>
<a class="sourceLine" id="cb13-25" data-line-number="25"><span class="kw">str</span>(subplots)</a></code></pre></div>
<pre><code>## 'data.frame':    4 obs. of  2 variables:
##  $ f   : Factor w/ 4 levels &quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;: 1 2 3 4
##  $ data:List of 4
##   ..$ :'data.frame': 10 obs. of  3 variables:
##   .. ..$ x  : num  0.4 0.422 0.444 0.467 0.489 ...
##   .. ..$ y  : num  0.469 0.452 0.491 0.498 0.492 ...
##   .. ..$ col: chr  &quot;#FFC5D0&quot; &quot;#FFC5D0&quot; &quot;#FFC5D0&quot; &quot;#FFC5D0&quot; ...
##   ..$ :'data.frame': 10 obs. of  3 variables:
##   .. ..$ x  : num  0.4 0.422 0.444 0.467 0.489 ...
##   .. ..$ y  : num  0.527 0.471 0.481 0.547 0.508 ...
##   .. ..$ col: chr  &quot;#FACABC&quot; &quot;#FACABC&quot; &quot;#FACABC&quot; &quot;#FACABC&quot; ...
##   ..$ :'data.frame': 10 obs. of  3 variables:
##   .. ..$ x  : num  0.4 0.422 0.444 0.467 0.489 ...
##   .. ..$ y  : num  0.469 0.478 0.459 0.471 0.548 ...
##   .. ..$ col: chr  &quot;#EDD0AD&quot; &quot;#EDD0AD&quot; &quot;#EDD0AD&quot; &quot;#EDD0AD&quot; ...
##   ..$ :'data.frame': 10 obs. of  3 variables:
##   .. ..$ x  : num  0.4 0.422 0.444 0.467 0.489 ...
##   .. ..$ y  : num  0.477 0.46 0.462 0.549 0.549 ...
##   .. ..$ col: chr  &quot;#DAD6A7&quot; &quot;#DAD6A7&quot; &quot;#DAD6A7&quot; &quot;#DAD6A7&quot; ...
##   ..- attr(*, &quot;class&quot;)= chr &quot;AsIs&quot;</code></pre>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb15-1" data-line-number="1"><span class="kw">ggplot</span>(d, <span class="kw">aes</span>(f,x)) <span class="op">+</span></a>
<a class="sourceLine" id="cb15-2" data-line-number="2"><span class="st">  </span><span class="kw">facet_wrap</span>(<span class="op">~</span>f, <span class="dt">nrow=</span><span class="dv">1</span>)<span class="op">+</span></a>
<a class="sourceLine" id="cb15-3" data-line-number="3"><span class="st">  </span><span class="kw">coord_polar</span>() <span class="op">+</span></a>
<a class="sourceLine" id="cb15-4" data-line-number="4"><span class="st">  </span><span class="kw">geom_point</span>()<span class="op">+</span></a>
<a class="sourceLine" id="cb15-5" data-line-number="5"><span class="st">  </span><span class="kw">geom_custom</span>(<span class="dt">data =</span> subplots, <span class="kw">aes</span>(<span class="dt">data =</span> data, <span class="dt">x =</span> f, <span class="dt">y =</span> <span class="dv">2</span>), </a>
<a class="sourceLine" id="cb15-6" data-line-number="6">              <span class="dt">grob_fun =</span> custom_grob) </a></code></pre></div>
<p><img src="" /><!-- --></p>
</div>



<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>
